package cn.community.system.web.controller;

import cn.community.system.query.AssociationDeviceLendReportQuery;
import cn.community.system.query.AssociationDeviceReturnReportQuery;
import cn.community.system.query.AssociationReportQuery;
import cn.community.system.service.IAssociationReportService;
import cn.community.system.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/dataAssociationReport")
public class AssociationReportController {

    @Autowired
    private IAssociationReportService associationReportService;

    @RequestMapping("/activityReportList")
    @RequiresPermissions(value = {"dataAssociationReport/activityReportList","社团-活动信息统计列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") AssociationReportQuery qo){
        model.addAttribute("page",associationReportService.selectAllActivityReport(qo));
        return "dataReport/association/activityReportList";
    }

    @RequestMapping("/deviceReportList")
    @RequiresPermissions(value = {"dataAssociationReport/deviceReportList","社团-设备借用统计列表"},logical = Logical.OR)
    public String deviceList(Model model, @ModelAttribute("qo") AssociationDeviceLendReportQuery qo){
        model.addAttribute("page",associationReportService.selectAllDeviceReport(qo));
        return "dataReport/association/deviceLendReportList";
    }

    @RequestMapping("/deviceReturnReportList")
    @RequiresPermissions(value = {"dataAssociationReport/deviceReturnReportList","社团-设备归还统计列表"},logical = Logical.OR)
    public String deviceReturnReportList(Model model, @ModelAttribute("qo") AssociationDeviceReturnReportQuery qo){
        model.addAttribute("page",associationReportService.selectReturnDeviceReport(qo));
        return "dataReport/association/deviceReturnReportList";
    }

    @RequestMapping("/activityReportListByBar") // 活动柱状图
    @RequiresPermissions(value = {"dataAssociationReport/activityReportListByBar","社团-活动统计柱状图"},logical = Logical.OR)
    public String listByBar(Model model, @ModelAttribute("qo") AssociationReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("activityNumbers"));
            yList2.add(map.get("activityTimes"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg(qo));
        return "dataReport/activityListByBar";
    }

    @RequestMapping("/activityReportListByPie")  // 活动个数饼图
    @RequiresPermissions(value = {"dataAssociationReport/activityReportListByPie","社团-活动个数饼图"},logical = Logical.OR)
    public String listByPie(Model model, @ModelAttribute("qo") AssociationReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("activityNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg(qo));//分组类型
        return "dataReport/activityListByPie";
    }

    @RequestMapping("/activityReportListByPie2")  // 活动总时长饼图
    @RequiresPermissions(value = {"dataAssociationReport/activityReportListByPie2","社团-活动时长饼图"},logical = Logical.OR)
    public String listByPie2(Model model, @ModelAttribute("qo") AssociationReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listAll(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("activityTimes"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg(qo));//分组类型
        return "dataReport/activityListByPie2";
    }



    @RequestMapping("/deviceReportListByBar") // 设备借用柱状图
    @RequiresPermissions(value = {"dataAssociationReport/deviceReportListByBar","社团-设备借用柱状图"},logical = Logical.OR)
    public String deviceListByBar(Model model, @ModelAttribute("qo") AssociationDeviceLendReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listAllDevice(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("deviceCount"));
            yList2.add(map.get("deviceNumbers"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg2(qo));
        return "dataReport/deviceListByBar";
    }

    @RequestMapping("/deviceReportListByPie")  // 设备借用次数饼图
    @RequiresPermissions(value = {"dataAssociationReport/deviceReportListByPie","社团-设备借用次数饼图"},logical = Logical.OR)
    public String deviceListByPie(Model model, @ModelAttribute("qo") AssociationDeviceLendReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listAllDevice(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceCount"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg2(qo));//分组类型
        return "dataReport/deviceListByPie";
    }

    @RequestMapping("/deviceReportListByPie2")  // 设备借用个数饼图
    @RequiresPermissions(value = {"dataAssociationReport/deviceReportListByPie2","社团-设备借用个数饼图"},logical = Logical.OR)
    public String deviceListByPie2(Model model, @ModelAttribute("qo") AssociationDeviceLendReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listAllDevice(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg2(qo));//分组类型
        return "dataReport/deviceListByPie2";
    }



    @RequestMapping("/deviceReturnReportListByBar") // 设备归还柱状图
    @RequiresPermissions(value = {"dataAssociationReport/deviceReturnReportListByBar","社团-设备归还柱状图"},logical = Logical.OR)
    public String deviceReturnListByBar(Model model, @ModelAttribute("qo") AssociationDeviceReturnReportQuery qo){
        //获取分页结果集
        //PageInfo pageInfo = customerReportService.selectCustomerReport(qo);
        //List<Map> list = pageInfo.getList();
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listReturnDevice(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储y轴的两个数据
        ArrayList yList1 = new ArrayList();
        ArrayList yList2 = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            yList1.add(map.get("deviceCount"));
            yList2.add(map.get("deviceNumbers"));
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        System.out.println(JSON.toJSONString(xList));
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList1",JSON.toJSONString(yList1));
        model.addAttribute("yList2",JSON.toJSONString(yList2));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg3(qo));
        return "dataReport/deviceListByBar2";
    }

    @RequestMapping("/deviceReturnReportListByPie")  // 设备借用次数饼图
    @RequiresPermissions(value = {"dataAssociationReport/deviceReturnReportListByPie","社团-设备借用次数饼图"},logical = Logical.OR)
    public String deviceReturnListByPie(Model model, @ModelAttribute("qo") AssociationDeviceReturnReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listReturnDevice(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceCount"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg3(qo));//分组类型
        return "dataReport/deviceReturnListByPie";
    }

    @RequestMapping("/deviceReturnReportListByPie2")  // 设备借用个数饼图
    @RequiresPermissions(value = {"dataAssociationReport/deviceReturnReportListByPie2","社团-设备借用个数饼图"},logical = Logical.OR)
    public String deviceReturnListByPie2(Model model, @ModelAttribute("qo") AssociationDeviceReturnReportQuery qo){
        //获取所有数据(不分页)
        List<Map> list = associationReportService.listReturnDevice(qo);
        //提供一个集合存储x轴的数据
        ArrayList xList = new ArrayList();
        //提供一个集合存储饼图的数据
        ArrayList yList = new ArrayList();
        //需要把数据转换为echart需要结构 x和y轴的数据要分开
        for (Map map : list) {
            xList.add(map.get("groupType"));
            HashMap<String, Object> temp = new HashMap<>();
            temp.put("name",map.get("groupType"));
            temp.put("value",map.get("deviceNumbers"));
            yList.add(temp);
        }
        //共享到页面(freemarker不能直接显示非字符串的数据(集合,时间))
        model.addAttribute("xList", JSON.toJSONString(xList));
        model.addAttribute("yList",JSON.toJSONString(yList));
        model.addAttribute("groupTypeName", MessageUtil.changAssociationMsg3(qo));//分组类型
        return "dataReport/deviceReturnListByPie2";
    }
}
